package com.course.oauth2.service;

import com.course.common.model.domain.SignInIdentity;
import com.course.common.model.pojo.Diners;
import com.course.common.util.AssertUtil;
import com.course.oauth2.mapper.DinersMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @PROJECT_NAME: course
 * @DESCRIPTION: 登录校验
 * @Author: 涂玄武
 * @DATE: 2021/6/21 14:08
 */
@Service
public class UserService implements UserDetailsService {

    @Resource
    private DinersMapper dinersMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        AssertUtil.isNotEmpty(username,"请输入用户名");
        Optional<Diners> optional = Optional.ofNullable(dinersMapper.selectByAccountInfo(username));
        if (!optional.isPresent()){
            throw new UsernameNotFoundException("用户名或密码错误，请重新输入!");
        }
        //初始化登录认证对象
        SignInIdentity signInIdentity = new SignInIdentity();
        //拷贝属性
        BeanUtils.copyProperties(optional.get(),signInIdentity);
        return signInIdentity;
    }
}
